package phantri.uit.menunhahang;

import java.util.ArrayList;

import phantri.uit.menunhahang.MonanActivity.MenuListAdapter.ViewHolder;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DouongActivity extends Activity {
	ArrayList<DouongObj> arraylistDouong;
	ArrayAdapter<DouongObj> arrayadapDouong;
	ListView listDouong;
	String strInfo;	
	protected Dialog onCreateDialog(int id) {
		Dialog d = null;
		AlertDialog.Builder[] builder = new AlertDialog.Builder[listDouong.getCount()];
			builder[id] = new AlertDialog.Builder(this);
			builder[id].setMessage(strInfo)
					.setCancelable(false)
					.setNegativeButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {									
									dialog.cancel();
								}
							});
			d = builder[id].create();
		return d;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.douong);
		arraylistDouong = new ArrayList<DouongObj>();
		listDouong = (ListView) findViewById(R.id.list_douong);
		try {
			GetDouong temp = new GetDouong();
			for (DouongObj Douongobj : temp.getDouong()) {
				arraylistDouong.add(Douongobj);
			}
			arrayadapDouong = new MenuListAdapter(this, arraylistDouong);
			listDouong.setAdapter(arrayadapDouong);

		} catch (Exception e) {
			e.toString();
		}
	}

	public class MenuListAdapter extends ArrayAdapter<DouongObj> {
		
		private ArrayList<DouongObj> items;
		private Activity context;

		public MenuListAdapter(Context context, ArrayList<DouongObj> items) {
			super(context, R.layout.listrow, items);
			this.items = items;
			this.context = (Activity) context;
		}

		public class ViewHolder {
			protected TextView text;
			protected TextView gia;
			protected TextView mota;
			protected Button tang;
			protected Button giam;
			protected Button datmon;
			protected Button chitiet;
			protected TextView soluong;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			
			View v = null;
			if (convertView == null) {
				LayoutInflater inflater = context.getLayoutInflater();
				v = inflater.inflate(R.layout.listrow, null);
				final ViewHolder vh = new ViewHolder();
				vh.text = (TextView) v.findViewById(R.id.listrow_txt);
				vh.gia = (TextView) v.findViewById(R.id.listrow_gia);
				vh.mota = (TextView) v.findViewById(R.id.listrow_mota);
				vh.tang = (Button) v.findViewById(R.id.btn_tang);
				vh.giam = (Button) v.findViewById(R.id.btn_giam);
				vh.datmon = (Button) v.findViewById(R.id.btn_datmon);
				vh.soluong = (TextView) v.findViewById(R.id.txt_soluong);
				vh.chitiet = (Button) v.findViewById(R.id.btn_chitiet);
				v.setTag(vh);
			} else {
				v = convertView;
			}
			final ViewHolder holder = (ViewHolder) v.getTag();
			holder.text.setText("\n" + "--"
					+ items.get(position).getName().toUpperCase() + "--");
			holder.gia.setText("-Giá: "
					+ Integer.parseInt(items.get(position).getPrice())
					+ " VNĐ/phần");
			holder.mota.setText("-Giới thiệu: " + "\n"
					+ items.get(position).getDescription());
			holder.soluong.setText(Integer.toString(items.get(position).getSoluong()));

			holder.tang.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub	
					int soluongtang = Integer.parseInt(holder.soluong.getText().toString());
					if (soluongtang < 99) {
						holder.tang.setClickable(true);
						soluongtang += 1;
						items.get(position).setSoluong(soluongtang);
						holder.soluong.setText(Integer.toString(items.get(position).getSoluong()));
					} else {
					}
				}
			});
			
			holder.giam.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int soluonggiam = Integer.parseInt((String) holder.soluong
							.getText().toString());
					if (soluonggiam > 0) {
						holder.giam.setClickable(true);
						soluonggiam -= 1;
						items.get(position).setSoluong(soluonggiam);
						holder.soluong.setText(Integer.toString(items.get(position).getSoluong()));
					} else {
					}
				}
			});
			holder.datmon.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent int_datmon = new Intent(DouongActivity.this,
							ChitietActivity.class);
					// startActivity(int_chitiet);
					String info;
					int soluong = Integer.parseInt((String) holder.soluong
							.getText());
					if (soluong != 0) {
						info = "Bạn đã chọn món " + "'"
								+ items.get(position).getName().toUpperCase()
								+ "', với số lượng " + holder.soluong.getText();

					} else {
						info = "Xin cho biết số lượng món " + "'"
								+ items.get(position).getName().toUpperCase()
								+ "' là bao nhiêu!";
					}
					Toast toast = Toast.makeText(context, info,
							Toast.LENGTH_SHORT);
					toast.show();

				}
			});
			holder.chitiet.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					strInfo = "--"+items.get(position).getName().toUpperCase()+":" + "\n" + items.get(position).getInfo();
					showDialog(position);
				}
			});

			return v;
		}
	}
	
}
